﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FlatFileParser.Core.Processor;
using FlatFileParser.Core.Enums;

namespace TestApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            LoadSampleText();
        }

        IEnumerable<ComplexType> complexTypeRecords = null;

        public class PersonListItem
        {
            public int ID { get; set; }
            public string DisplayValue { get; set; }
        }

        public void LoadSampleText()
        {
            var processor = new FileProcessor<SimpleText>();
            processor.Setup.Add(ProcessingSetup.SkipEmptyLines);
            dgvSimpleText.DataSource = processor.ProcessFile("SimpleText.txt");

            var processor2 = new FileProcessor<ProductList>();
            dgvProductList.DataSource = processor2.ProcessFile("ProductList.txt");
            dgvProductList.Columns["Amount"].DefaultCellStyle.Format = "c";
            dgvProductList.Columns["Amount"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            var processor3 = new FileProcessor<PartialFile>();
            dgvPartialFile.DataSource = processor3.ProcessFile("PartialFile.txt");

            var processor4 = new FileProcessor<ComplexType>();
            complexTypeRecords = processor4.ProcessFile("ComplexType.txt");

            lstPeople.DataSource = (from record in complexTypeRecords
                                   select new PersonListItem { ID=record.Person.PersonId , DisplayValue = record.Person.LastName.Trim() + ", " + record.Person.FirstName.Trim() }).ToList();

            lstPeople.ValueMember = "ID";
            lstPeople.DisplayMember = "DisplayValue";
        }

        private void lstPeople_SelectedIndexChanged(object sender, EventArgs e)
        {
            var record = complexTypeRecords.Where(x => x.Person.PersonId == ((PersonListItem)lstPeople.SelectedItem).ID).FirstOrDefault();

            if (record != null)
            {
                lblName.Text = "Name: " + record.Person.LastName.Trim() + ", " + record.Person.FirstName.Trim();
                lblAge.Text = "Age: " + record.Person.Age.ToString();
                lblUsername.Text = "Username: " + record.Person.Username.Trim();
                lblAddress.Text = "Address: " + Environment.NewLine + record.Address.Street.Trim() + Environment.NewLine + record.Address.City.Trim() + ", " + record.Address.State.Trim() + " " + record.Address.ZipCode;
            }
        }
    }
}
